﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
///HotelUser 的摘要说明
/// </summary>
public class HotelUser : User
{

   // Component mUser;
    Component mComponent;

    string account;

    string acpassword ;
    string userType ;

    public string Account
    {
        get { return account; }
        set { account = value; }
    }
    public string Acpassword
    {
        get { return acpassword; }
        set { acpassword = value; }
    }
    public string UserType
    {
        get { return userType; }
        set { userType = value; }
    }

	public HotelUser():base()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public HotelUser( //Component mUser,
             Component mHotel):base()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
       // this.mUser = mUser;
        this.mComponent = mHotel;
        
    }

    public HotelUser(
        //Component mUser,
        Component mHotel,
        string idcard,
        string name,
        string sex,
        string birthday,
        string account,
        string acpassword,
        string userType)
        : base()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
        //this.mUser = mUser;
        base.Idcard = idcard;
        base.Sex = sex;
        base.Name = name;
        base.Birthday = birthday;
        this.mComponent = mHotel;
        this.account = account;
        this.acpassword = acpassword;
        this.userType = userType;

    }

    public override string insert(SqlConnection sqlcon)
    {

        Hotel mHotel = (Hotel)this.mComponent;
       // User mUser = (User)this.mUser;

        string[] data ={
                          SqlFormat.ap(mHotel.Name),
                          mHotel.ProvinceID,
                          mHotel.CityID,
                          mHotel.CountyID,
                          SqlFormat.ap(mHotel.Address),
                          SqlFormat.ap(mHotel.Telephonenumber),
                          SqlFormat.ap(base.Idcard),
                          SqlFormat.ap(base.Name),
                          base.Sex,
                          SqlFormat.ap(base.Birthday),
                          SqlFormat.ap(account),
                          SqlFormat.ap(acpassword),
                          userType
                      };

        string sqlcmdstr = SqlFormat.exeProcedureNotap("inputHotelUser", data);

        try
        {
            SqlCommand sqlCmd = new SqlCommand(sqlcmdstr, sqlcon);

            if (sqlCmd.ExecuteNonQuery() == 0)
            {
                return null;
            }
        }
        catch (System.Exception ex)
        {
            return null;
        }
        
  

        return "ok";
    }

    /************************************************************************/
    /* 根据账户判断该账户是否存在                                                                     */
    /************************************************************************/
    public static bool isExist(System.Data.SqlClient.SqlConnection sqlcon, string mAccount)
    {
        string sqlcmd = " SELECT IDcard FROM HotelUser WHERE account=" + SqlFormat.ap(mAccount) + ";";
        try
        {
            SqlDataAdapter sqladt = new SqlDataAdapter();
            DataTable dts = new DataTable();
            sqladt.SelectCommand = new SqlCommand(sqlcmd, sqlcon);
            sqladt.Fill(dts);

            if (dts.Rows.Count != 1)
            {
                return false;
            }

            return true;
        }
        catch (System.Exception ex)
        {
            return false;
        }
        
    }

    public static object[] getDataByIDAndPsw(System.Data.SqlClient.SqlConnection sqlcon, string mAccount, string mPassword)
    {
        string sqlcmd = " SELECT * FROM HotelUser WHERE "
            + "account=" + SqlFormat.ap(mAccount)
            +"AND acpassword="+ SqlFormat.ap(mPassword) + ";";

        try
        {
            SqlDataAdapter sqladt = new SqlDataAdapter();
            DataTable dts = new DataTable();
            sqladt.SelectCommand = new SqlCommand(sqlcmd, sqlcon);
            sqladt.Fill(dts);

            if (dts.Rows.Count != 1)
            {
                return null;
            }


            return dts.Rows[0].ItemArray;
        }
        catch (System.Exception ex)
        {
            return null;
        }
       
    }
}